import { $set } from "@feng3d/serialization";
import { watcher } from "@feng3d/watcher";
import { PlaneGeometry } from "../../thirdparty/three/imports";
import { BufferGeometry3D } from "../core/BufferGeometry3D";

export class PlaneGeometry3D extends BufferGeometry3D
{
    width = 1;
    height = 1;
    widthSegments = 1;
    heightSegments = 1;

    get _geometry()
    {
        if (!this.__geometry)
        {
            this.__geometry = new PlaneGeometry(this.width, this.height, this.widthSegments, this.heightSegments);
        }

        return this.__geometry;
    }
    declare protected __geometry: PlaneGeometry;

    constructor()
    {
        super();
        watcher.watchs(this as PlaneGeometry3D, ["width", "height", "widthSegments", "heightSegments"], this._invalidate, this);
    }

    destroy()
    {
        watcher.unwatchs(this as PlaneGeometry3D, ["width", "height", "widthSegments", "heightSegments"], this._invalidate, this);
    }

    clone()
    {
        const planeGeometry3D = $set(new PlaneGeometry3D(), { width: this.width, height: this.height, widthSegments: this.widthSegments, heightSegments: this.heightSegments });
        planeGeometry3D.__geometry = this._geometry.clone() as any;

        return planeGeometry3D;
    }
}
